/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import java.io.File;
import java.io.FileWriter;

import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author Ivan
 */
public class XML {

    public static Document readDocument(String fileFullName) throws IOException, JDOMException {
        SAXBuilder builder = new SAXBuilder();
        Document anotherDocument = builder.build(new File(fileFullName));
        return anotherDocument;
    }

    /**
     * Remove the child being right after the rootElement.
     * @param myDocument
     * @param childName the child being right after the rootElement
     * @return isSuccess
     */
    public static boolean removeChildElement(Document myDocument, String childName) {
        Element clientInfos = myDocument.getRootElement();
        return clientInfos.removeChild(childName);
    }

    public static void outputDocumentToFile(Document myDocument, String fullFileName) throws IOException {
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());

        FileWriter writer = new FileWriter(fullFileName, false);
        outputter.output(myDocument, writer);
        writer.close();
    }
}
